﻿using System;
using System.Collections;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace SiteFox.WebParts.Blog
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]

    public class PostWP : WebPart
    {
        #region PublicProperies
        private string _PostsUrl = "Default.aspx";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("")]
        [WebDisplayName("Posts url")]
        public string PostsUrl
        {
            get { return _PostsUrl; }
            set { _PostsUrl = value; }
        }

        private string _XsltUrl = "~/System/Design/xslt/WebParts/Blog/Post.xslt";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("")]
        [WebDisplayName("XSL-T url")]
        public string XsltUrl
        {
            get { return _XsltUrl; }
            set { _XsltUrl = value; }
        }
        #endregion

        protected override void CreateChildControls()
        {
            Controls.Clear();
            if (HttpContext.Current.Request.QueryString["Id"] != null) 
            {
                int _QueryId = -1;
                try { _QueryId = Convert.ToInt32(HttpContext.Current.Request.QueryString["Id"].ToString()); }
                catch { }
                if (_QueryId > -1) 
                { 
                    var _Post = SiteFox.Business.Manager.BlogPost.GetByID(_QueryId);
                    if (_Post != null) 
                    { 
                        Xml _Xml = new Xml();
                        _Xml.TransformSource = _XsltUrl;
                        XmlDocument _XmlDocument = new XmlDocument();
                        XmlDeclaration _XmlDeclaration = _XmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
                        XmlElement _XmlPost = _XmlDocument.CreateElement("Post");
                        _XmlDocument.InsertBefore(_XmlDeclaration, _XmlDocument.DocumentElement);
                        _XmlDocument.AppendChild(_XmlPost);
                        _XmlPost.SetAttribute("Id", _Post.ID.ToString());
                        _XmlPost.SetAttribute("Date", _Post.Created.Value.ToShortDateString());
                        _XmlPost.SetAttribute("Time", _Post.Created.Value.ToShortTimeString());
                        _XmlPost.SetAttribute("CreatedBy", _Post.CreatedBy);
                        _XmlPost.SetAttribute("Comments", _Post.BlogComment.Count.ToString());
                        XmlElement _XmlPostTitle = _XmlDocument.CreateElement("Title");
                        _XmlPostTitle.InnerText = _Post.Title;
                        _XmlPost.AppendChild(_XmlPostTitle);
                        XmlElement _XmlPostText = _XmlDocument.CreateElement("Text");
                        _XmlPostText.InnerText = _Post.Text;
                        _XmlPost.AppendChild(_XmlPostText);
                        _Xml.DocumentContent = _XmlDocument.InnerXml;
                        Controls.Add(_Xml);
                    }
                }
            }
        }
    }
}
